package cn.tedu.flower.manage.content.controller;

import cn.tedu.flower.manage.common.pojo.vo.PageData;
import cn.tedu.flower.manage.common.security.CurrentPrincipal;
import cn.tedu.flower.manage.common.web.JsonResult;
import cn.tedu.flower.manage.content.pojo.param.FadebackAddNewParam;
import cn.tedu.flower.manage.content.pojo.param.FadebackUpdateParam;
import cn.tedu.flower.manage.content.pojo.param.MemberUpdateParam;
import cn.tedu.flower.manage.content.pojo.vo.FadebackListItemVO;
import cn.tedu.flower.manage.content.pojo.vo.FadebackStandardVO;
import cn.tedu.flower.manage.content.service.IFadebackService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/content/fadeback")
@Validated
@Api(tags = "1.2. 反馈信息管理")
public class FadebackController {
    @Autowired
    IFadebackService fadebackService;

    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/content/article/add-new')")
    @ApiOperation("添加反馈信息")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                             @ApiIgnore HttpServletRequest request,
                             @Valid FadebackAddNewParam fadebackAddNewParam) {
        log.debug("开始处理【添加反馈信息】的请求，参数：{}", fadebackAddNewParam);
        String remoteAddr = request.getRemoteAddr();
        fadebackService.addNew(currentPrincipal, remoteAddr, fadebackAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/content/article/delete')")
    @ApiOperation("根据ID删除反馈信息")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "反馈信息ID", required = true, dataType = "long")
    })
    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交有效的反馈信息ID值！") Long id) {
        log.debug("开始处理【根据ID删除反馈信息】的请求，参数：{}", id);
        fadebackService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/content/article/update')")
    @ApiOperation("修改反馈信息")
    @ApiOperationSupport(order = 312)
    public JsonResult update(FadebackUpdateParam fadebackUpdateParam){
        log.debug("开始处理【修改反馈信息】的请求，参数：{}",fadebackUpdateParam);
        fadebackService.update(fadebackUpdateParam);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/content/article/read')")
    @ApiOperation("根据ID查询反馈信息详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "反馈信息ID", required = true, dataType = "long")
    })
    public JsonResult selectById(
            @PathVariable @Range(min = 1, message = "请提交有效的反馈信息ID值！") Long id) {
        log.debug("开始处理【根据ID查询反馈信息详情】的请求，参数：{}", id);
        FadebackStandardVO queryResult = fadebackService.selectById(id);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("")
    @PreAuthorize("hasAuthority('/content/article/read')")
    @ApiOperation("查询反馈信息列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "long"),
    })
    public JsonResult list(@Range(min = 1, message = "请提交有效的页码值！") Integer page) {
        log.debug("开始处理【查询反馈信息列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<FadebackListItemVO> pageData = fadebackService.selectAll(pageNum);
        return JsonResult.ok(pageData);
    }

}
